import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {STColumn} from '@delon/abc';
import {SFSchema} from '@delon/form';
import {BaseGridComponent, BaseService} from '@sb/base';
import {SysBackupTemplateParamService} from '../service/sys-backup-template-param.service';
import {SysBackupTemplateParam} from '../entity/sys-backup-template-param';
import {
    SysCodeCategorySelectAlainComponent,
    SysCodeCategorySelectAlainSchema
} from '../../../sys-code/sys-code-category-select-alain/sys-code-category-select-alain.component';

@Component({
    selector: 'sys-backup-template-param',
    templateUrl: './sys-backup-template-param.component.html',
    styleUrls: ['./sys-backup-template-param.component.less'],
    providers: [
        {
            provide: BaseService,
            useClass: SysBackupTemplateParamService
        }
    ]
})
export class SysBackupTemplateParamComponent implements OnInit {

    private _templateId: string;

    get templateId(): string {
        return this._templateId;
    }

    @Input()
    set templateId(value: string) {
        this._templateId = value;

        this.baseParam.templateId = value;
        if (value && this.bg) {
            this.bg.reload();
        }
    }

    baseParam: SysBackupTemplateParam = {};

    @ViewChild(BaseGridComponent, {static: false})
    bg: BaseGridComponent;

    columns: STColumn[] = [
        {
            title: '',
            type: 'checkbox',
            index: 'paramId'
        }, {
            title: '编码',
            index: 'paramCode',
            width: '30%'
        }, {
            title: '名称',
            index: 'paramName',
            width: '30%'
        }, {
            title: '展示方式',
            index: 'display',
            width: '30%'
        }
    ];

    schema: SFSchema = {
        properties: {
            paramCode: {
                type: 'string',
                title: '编码',
                minLength: 2
            },
            paramName: {
                type: 'string',
                title: '名称',
                minLength: 2
            },
            display: {
                type: 'string',
                title: '展示方式',
                enum: [{
                    label: '数字输入框',
                    value: 'number'
                }, {
                    label: '单行文本框',
                    value: 'text'
                }, {
                    label: '密码输入框',
                    value: 'password'
                }, {
                    label: '多行文本框',
                    value: 'textarea'
                }, {
                    label: '字典选择框',
                    value: 'syscode'
                }, {
                    label: '开关',
                    value: 'boolean',
                    disabled: true
                }],
                ui: {
                    widget: 'select',
                }
            },
            minRows: {
                type: 'number',
                title: '最小行数',
                ui: {
                    visibleIf: {
                        display: ['textarea']
                    }
                },
                default: 3
            },
            maxRows: {
                type: 'number',
                title: '最大行数',
                ui: {
                    visibleIf: {
                        display: ['textarea']
                    }
                },
                default: 6
            },
            sysCodeModuleCode: {
                type: 'string',
                title: '字典模块',
                ui: {
                    widget: SysCodeCategorySelectAlainComponent.KEY,
                    multiple: false,
                    visibleIf: {
                        display: ['syscode']
                    },
                } as SysCodeCategorySelectAlainSchema,
            },
            // paramValueAttachment: {
            //     type: 'string',
            //     title: '附件',
            //     ui: {
            //         widget: SysAttachmentUploadAlainComponent.KEY,
            //         multiple: false,
            //         visibleIf: {
            //             display: ['syscode']
            //         },
            //     } as SysAttachmentUploadSchema,
            // },
            // paramValueSysCode: {
            //     type: 'string',
            //     title: '字典项',
            //     ui: {
            // widget: SysCodeTreeSelectAlainComponent.KEY,
            //         multiple: false,
            //         visibleIf: {
            //             valueType: ['SYS_CODE']
            //         },
            //     } as SysCodeTreeSelectAlainSchema,
            // },
            defaultValue: {
                type: 'string',
                title: '默认值',
                description: '如果是文本，请使用"引用。例如："mysqldump"'
            }, description: {
                type: 'string',
                title: '提示',
            },
        },
        ui: {
            spanLabelFixed: 100
        },
        required: ['paramCode', 'paramName', 'display']
    };

    constructor() {
    }

    ngOnInit() {
    }

}
